//
//  NSString+Extent.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/7/21.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "NSString+Extent.h"

@implementation NSString (Extent)

+ (NSString *)errorStringWithCode:(NSString *)errorCode {
    
    NSString *errorString = nil;
    
    switch (errorCode.integerValue) {
        case 3001:
            errorString = @"参数为空";
            break;
            
        case 3002:
            errorString = @"数据异常";
            break;
            
        case 3003:
            errorString = @"无返回结果";
            break;
            
        case 3004:
            errorString = @"程序流程异常";
            break;
            
        case 3006:
            errorString = @"需要返回一条数据，但实际返回多条，请检查重复数据";
            break;
            
        case 4001:
            errorString = @"账号不存在或密码错误";
            break;
            
        case 4002:
            errorString = @"密码错误";
            break;
            
        case 4003:
            errorString = @"旧密码错误";
            break;
            
        case 4004:
            errorString = @"两次密码不一致";
            break;
            
        case 4005:
            errorString = @"账号已存在";
            break;
            
        case 4006:
            errorString = @"请重新登陆";
            break;
            
        case 4007:
            errorString = @"注册类型不匹配此方法";
            break;
            
        case 4008:
            errorString = @"用户已加入俱乐部";
            break;
            
        case 4009:
            errorString = @"活动用户已报名";
            break;
            
        case 4010:
            errorString = @"活动报名会员数据不存在，请先对会员进行报名";
            break;
            
        case 4012:
            errorString = @"活动有会员限制，请先加入俱乐部";
            break;
            
        case 4013:
            errorString = @"非俱乐部会员或未通过审核，为非法操作";
            break;
            
        case 4014:
            errorString = @"身份证号错误";
            break;
            
        case 4018:
            errorString = @"此活动该报名者已报名";
            break;
            
        case 467:
            errorString = @"请求校验验证码频繁";
            break;
            
        case 468:
            errorString = @"验证码错误";
            break;
            
        case 457:
            errorString = @"手机号码格式错误";
            break;
            
        case 456:
            errorString = @"手机号码为空";
            break;
            
        case 466:
            errorString = @"请求校验的验证码为空";
            break;
            
        default:
            break;
    }
    
    return errorString;
}

#pragma mark - **************** 获取括号内的字符串
/**
 *  字符串截取：获取字符串中用小括号括住的位置
 */
- (NSString *)handleStringGetAmongInSmallBrace {
    
    NSMutableString * muStr = [NSMutableString stringWithString:self];
    while (1) {
        NSRange range = [muStr rangeOfString:@"("];
        
        NSRange range1 = [muStr rangeOfString:@")"];
        
        if (range.location != NSNotFound) {
            NSInteger loc = range.location;
            NSInteger len = range1.location - range.location;
            NSRange amongRange = NSMakeRange(loc + 1, len - 1);
            NSString *amongString = [muStr substringWithRange:amongRange];
            
            return amongString;
            //            [muStr deleteCharactersInRange:NSMakeRange(loc, len + 1)];
        }else{
            break;
        }
    }
    
    return muStr;
}

/**
 *  字符串截取：获取字符串中用中括号括住的位置
 */
- (NSString *)handleStringGetAmongInMidBrace {
    
    NSMutableString * muStr = [NSMutableString stringWithString:self];
    while (1) {
        NSRange range = [muStr rangeOfString:@"["];
        
        NSRange range1 = [muStr rangeOfString:@"]"];
        
        if (range.location != NSNotFound) {
            NSInteger loc = range.location;
            NSInteger len = range1.location - range.location;
            NSRange amongRange = NSMakeRange(loc + 1, len - 1);
            NSString *amongString = [muStr substringWithRange:amongRange];
            
            return amongString;
            //            [muStr deleteCharactersInRange:NSMakeRange(loc, len + 1)];
        }else{
            break;
        }
    }
    
    return muStr;
}

/**
 *  字符串截取：获取字符串中用大括号括住的位置
 */
- (NSString *)handleStringGetAmongInBigBrace {
    
    NSMutableString * muStr = [NSMutableString stringWithString:self];
    while (1) {
        NSRange range = [muStr rangeOfString:@"{"];
        
        NSRange range1 = [muStr rangeOfString:@"}"];
        
        if (range.location != NSNotFound) {
            NSInteger loc = range.location;
            NSInteger len = range1.location - range.location;
            NSRange amongRange = NSMakeRange(loc + 1, len - 1);
            NSString *amongString = [muStr substringWithRange:amongRange];
            
            return amongString;
            //            [muStr deleteCharactersInRange:NSMakeRange(loc, len + 1)];
        }else{
            break;
        }
    }
    
    return muStr;
}

#pragma mark - 根据两边字符获取中间字符
- (NSString *)handleStringFromeStartString:(NSString *)startString toEndString:(NSString *)endString {
    NSRange range = [self rangeOfString:startString];
    NSString *string;
    if (range.location != NSNotFound) {
        string = [self substringFromIndex:range.location + range.length];
    }
    
    range = [string rangeOfString:endString];
    if (range.location != NSNotFound) {
        string = [string substringToIndex:range.location];
    }
    return  string;
    
}

//字典转json字符串
+ (NSString*)dictionaryToJson:(id)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
